﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace Bingo
{
    public partial class Bingo : GlassForm
    {
        #region Attributes

        private Size sBingo = new Size(100, 50);
        private Size sControl = new Size(800, 400); 

        #region UserControl

        private Help.ucAbout myAbout = null;
        private Tools.ucOption myOption = null;

        private SSQ.ucNumber mySSQNumber = null;
        private SSQ.ucVerify myVerify = null;
        private SSQ.ucForeCast mySSQForeCast = null;
        private SSQ.ucRate mySSQRate = null;

        private ThreeD.ucNumber my3DNumber = null;
        private ThreeD.ucForeCast my3DForeCast = null;
        private Lottery.ucNumber myLotteryNumber = null;
        private Lottery.ucForeCast myLotteryForeCast = null;

        #endregion

        public Thread myThread = null;

        #endregion

        #region Bingo

        public Bingo()
        {
            InitializeComponent();
            sBingo = this.ClientSize;
        }

        private void Bingo_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            if (myThread != null && myThread.IsAlive)
            {
                if (MessageBox.Show("正在进行操作，确定要退出吗？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }
                myThread.Abort();
            }
            e.Cancel = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.menuStrip1.BackColor = this.BackColor;
        }

        #endregion

        #region Event

        #region Control

        private void pMain_ControlRemoved(object sender, ControlEventArgs e)
        {
            SetBingoSize();
        }

        private void pMain_ControlAdded(object sender, ControlEventArgs e)
        {
            SetBingoSize();
        }

        #endregion

        #region ToolStripMenuItem

        #region File

        private void toolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region SSQ

        private void toolStripMenuItem_SSQNumber_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(mySSQNumber))
            {
                mySSQNumber = new SSQ.ucNumber();
                mySSQNumber.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(mySSQNumber);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            mySSQNumber.BringToFront();
        }

        private void toolStripMenuItem_Verify_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(myVerify))
            {
                myVerify = new SSQ.ucVerify();
                myVerify.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(myVerify);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            myVerify.BringToFront();
        }

        private void toolStripMenuItem_SSQForeCast_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(mySSQForeCast))
            {
                mySSQForeCast = new SSQ.ucForeCast();
                mySSQForeCast.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(mySSQForeCast);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            mySSQForeCast.BringToFront();
        }

        private void toolStripMenuItem_SSQRate_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(mySSQRate))
            {
                mySSQRate = new SSQ.ucRate();
                mySSQRate.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(mySSQRate);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            mySSQRate.BringToFront();
        }

        #endregion

        #region 3D

        private void toolStripMenuItem_3DNumber_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(my3DNumber))
            {
                my3DNumber = new ThreeD.ucNumber();
                my3DNumber.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(my3DNumber);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            my3DNumber.BringToFront();
        }

        private void toolStripMenuItem_3DForeCast_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(my3DForeCast))
            {
                my3DForeCast = new ThreeD.ucForeCast();
                my3DForeCast.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(my3DForeCast);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            my3DForeCast.BringToFront();
        }

        #endregion

        #region Lottery

        private void toolStripMenuItem_LotteryNumber_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(myLotteryNumber))
            {
                myLotteryNumber = new Lottery.ucNumber();
                myLotteryNumber.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(myLotteryNumber);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            myLotteryNumber.BringToFront();
        }

        private void toolStripMenuItem_LotteryForeCast_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(myLotteryForeCast))
            {
                myLotteryForeCast = new Lottery.ucForeCast();
                myLotteryForeCast.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(myLotteryForeCast);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            myLotteryForeCast.BringToFront();
        }

        #endregion

        #region Tools

        private void toolStripMenuItem_Option_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(myOption))
            {
                myOption = new Tools.ucOption();
                myOption.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(myOption);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title; 
            myOption.BringToFront();
        }

        #endregion

        #region Help

        private void ToolStripMenuItem_About_Click(object sender, EventArgs e)
        {
            if (!this.pMain.Contains(myAbout))
            {
                myAbout = new Help.ucAbout();
                myAbout.Dock = DockStyle.Fill;
                this.pMain.Controls.Add(myAbout);
            }

            this.Text = ((ToolStripMenuItem)sender).Tag + " - " + ((ToolStripMenuItem)sender).OwnerItem.Tag + " - " + ((AssemblyTitleAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false))[0]).Title;
            myAbout.BringToFront();
        }

        #endregion

        #endregion

        #region ContextMenuStrip

        private void tsmiMain_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        #endregion

        #endregion

        #region Method

        private void SetBingoSize()
        {
            if (this.pMain.Controls.Count == 0)
            {
                this.ClientSize = sBingo;
                Point p = new Point(this.Location.X + (sControl.Width - sBingo.Width) / 2, this.Location.Y + sControl.Height / 2);
                if (p.X >= SystemInformation.WorkingArea.Width || p.Y >= SystemInformation.WorkingArea.Height) this.Location = new Point(10, 10);
                else this.Location = p;
                this.MaximizeBox = false;
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
            }
            else if (this.pMain.Controls.Count == 1 && this.ClientSize == sBingo)
            {
                this.ClientSize = new Size(sControl.Width, sBingo.Height + sControl.Height);
                Point p = new Point(this.Location.X + (sBingo.Width - sControl.Width) / 2, this.Location.Y - sControl.Height / 2);
                if (p.X < 0 || p.Y < 0) this.Location = new Point(10, 10);
                else this.Location = p;
                this.MaximizeBox = true;
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
        }

        #endregion
    }
}
